home *** CD-ROM | disk | FTP | other *** search
- /********************************************************/
- /* apidentd.c v1.0 */
- /* P.Toilon (zorthrax@id-net.fr) */
- /* 29/10/1998 IDNET - Nancy - FRANCE */
- /* Apidentd is released into the Public Domain */
- /* The original file 'apidentd.c' has 2046 bytes */
- /********************************************************/
-
- #include <stdio.h>
- #include <netdb.h>
- #include <signal.h>
- #include <sys/time.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
-
- int open_listener_socket(port,maxusers,timeout)
- int port,maxusers,timeout;
- {
- time_t tvey;
- struct sockaddr_in in;
- int fd,rt,sz=sizeof(struct sockaddr_in);
-
- if((fd=socket(AF_INET,SOCK_STREAM,0))<0)
- return(-1);
-
- memset(&in,0,sz);
- in.sin_family=AF_INET;
- in.sin_addr.s_addr=INADDR_ANY;
- in.sin_port=htons(port);
-
- time(&tvey);
-
- while((rt=bind(fd,(struct sockaddr *)&in,sz))<0 && (time(0)-tvey)<timeout)
- sleep(1);
-
- if(rt<0 || listen(fd,maxusers)<0)
- {
- close(fd);
- return(-1);
- }
-
- return(fd);
- }
-
- /*
- ** The requester (e.g. irc server) asks :
- ** 6193, 23 : USERID : UNIX : Apircuser
- ** We may return :
- ** 6193 , 23 : USERID : UNIX : ap071
- */
-
- main()
- {
- struct sockaddr_in uin;
- char *ptr1,*ptr2,frame[1024];
- int cpt,nb,fd,mfd,osz,port1,port2;
-
- if((mfd=open_listener_socket(113,1,120))<0)
- {
- printf("\nSocket not openable. (Maybe another ident daemon yet running)\n");
- exit();
- }
-
- printf("\nApIdentd ready\n");
- fflush(stdout);
-
- cpt=0;
- osz=sizeof(struct sockaddr_in);
-
- /* If somebody comes, accept returns a positive value */
- while(1)
- {
- if((fd=accept(mfd,(struct sockaddr *)&uin,&osz))>2)
- {
- if((nb=recv(fd,frame,1024,0))>5)
- {
- ptr1=frame;
- ptr2=(char *)strchr(frame,',');
-
- if(ptr2)
- {
- ptr2++;
-
- while(*ptr1==' ') ptr1++;
- while(*ptr2==' ') ptr2++;
-
- port1=atoi(ptr1);
- port2=atoi(ptr2);
-
- sprintf(frame,"%d , %d : USERID : UNIX : ap%.3d\n",port1,port2,++cpt%1000);
- send(fd,frame,strlen(frame),0);
- }
- }
- close(fd);
- }
- }
- }
-